//
//  UIButton+Extension.swift
//  3月5的项目
//
//  Created by 哦哦 on 16/3/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
extension UIButton {

// 背景图片 + 图片的按钮的显示样式
class func buttonWithImage(imageName: String, backImageName: String) -> UIButton {
    let btn = UIButton()
    btn.setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
    btn.setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
    btn.setImage(UIImage(named: imageName), forState: .Normal)
    btn.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
    //自适应大小
    btn.sizeToFit()
    return btn
}


//背景图片 + 文字 样式的按钮
class func buttonWithtitle(title: String ,backImageName: String? ,textColor: UIColor,fontSize: CGFloat,imageName: String? = nil) -> UIButton {
    let btn = UIButton()
    //这是背景图片
    //stretch  撑大 
//    if backImageName != nil {
    if backImageName != nil {
        
        let capW = Int(UIImage(named:backImageName!)!.size.width * 0.5)
        let capH = Int(UIImage(named:backImageName!)!.size.height * 0.5)
        
        btn.setBackgroundImage(UIImage(named:backImageName!)!.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
    }
    if imageName != nil {
        btn.setImage(UIImage(named: imageName!), forState: .Normal)
    }
    
//    let capW = Int(UIImage(named:backImageName)!.size.width * 0.5)
//    let capH = Int(UIImage(named:backImageName)!.size.height * 0.5)
//    
//    btn.setBackgroundImage(UIImage(named:backImageName)!.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
    
    
//    btn.setBackgroundImage(UIImage(named:backImageName), forState: .Normal)
    btn.setTitle(title, forState: .Normal)
    btn.setTitleColor(textColor, forState: .Normal)
    btn.titleLabel?.font = UIFont.systemFontOfSize(fontSize)
    btn.sizeToFit()
    return btn
}
}
